﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Ink;

namespace InkCanvasControls
{
    /// <summary>
    /// Interaction logic for PenStyles.xaml
    /// </summary>
    public partial class PenStyles : Window, INotifyPropertyChanged
    {
        #region enums
        public enum ToolStyle {NONE, PEN, HIGHLIGHTER, STROKE_ERASER, POINT_ERASER, LASSO};
        #endregion

        #region UI bound properties
        public Color CustomColourSelected { get; set; }
        #endregion

        #region Constructor
        public PenStyles()
        {
            InitializeComponent();

            Closed += new EventHandler(PenStyles_Closed);

            BlackColour.IsChecked = true;
            ThinWidth.IsChecked = true;
            SquareTip.IsChecked = true;
            YellowColour.IsChecked = true;

            DataContext = this;
        }
        #endregion

        #region Window UI event handlers
        void PenStyles_Closed(object sender, EventArgs e)
        {
            Visibility = Visibility.Collapsed;
        }
        #endregion

        #region Show appropriate style pallet
        private ToolStyle _currentTool = ToolStyle.NONE;
        public ToolStyle CurrentTool
        {
            get
            {
                return _currentTool;
            }
            set
            {
                _currentTool = value;
                NotifyPropertyChanged("PenStyleVisibility");
                NotifyPropertyChanged("HighliterStyleVisibility");
                NotifyPropertyChanged("EraserStyleVisibility");
                NotifyPropertyChanged("LassoStyleVisibility");
            }
        }
        public Visibility PenStyleVisibility
        {
            get
            {
                return CurrentTool == ToolStyle.PEN ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public Visibility HighliterStyleVisibility
        {
            get
            {
                return CurrentTool == ToolStyle.HIGHLIGHTER ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public Visibility StrokeEraserStyleVisibility
        {
            get
            {
                return CurrentTool == ToolStyle.STROKE_ERASER ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public Visibility PointEraserStyleVisibility
        {
            get
            {
                return CurrentTool == ToolStyle.POINT_ERASER ? Visibility.Visible : Visibility.Collapsed;
            }
        }


        #endregion

        #region Handle style changes
        public Brush SelectedPenBrush { get; set; }
        private void PenColourButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn != null)
            {
                Line line = btn.Content as Line;
                if (line != null)
                {
                    SelectedPenBrush = line.Stroke;
                    OnPenColourChanged((Color)SelectedPenBrush.GetValue(SolidColorBrush.ColorProperty));
                    NotifyPropertyChanged("SelectedPenBrush");
                }
            }
        }

        public Brush SelectedHiliteBrush { get; set; }
        private void HiliteColourButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn != null)
            {
                Line line = btn.Content as Line;
                if (line != null)
                {
                    SelectedHiliteBrush = line.Stroke;
                    OnHiliteColourChanged((Color)SelectedHiliteBrush.GetValue(SolidColorBrush.ColorProperty));
                    NotifyPropertyChanged("SelectedHiliteBrush");
                }
            }
        }


        public double SelectedThickness { get; set; }
        private void WidthButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn != null)
            {
                Line line = btn.Content as Line;
                if (line != null)
                {
                    SelectedThickness = line.StrokeThickness;
                    OnThicknessChanged(SelectedThickness);
                    NotifyPropertyChanged("SelectedThickness");
                }
            }
        }

        public StylusTip SelectedTip { get; set; }
        private void TipButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn != null)
            {
                Shape shape = sender as Shape;
                if (shape != null)
                {
                    if (shape is Rectangle)
                    {
                        SelectedTip = StylusTip.Rectangle;
                    }
                    else
                    {
                        SelectedTip = StylusTip.Ellipse;
                    }
                    OnTipChanged(SelectedTip);
                    NotifyPropertyChanged("SelectedTip");
                }
            }
        }
        #endregion

        #region Attribute changed event support
        public event PenColourChangedEventHandler PenColourChanged;
        public delegate void PenColourChangedEventHandler(Color colour);
        public void OnPenColourChanged(Color colour)
        {
            if (PenColourChanged != null)
            {
                PenColourChanged(colour);
            }
        }

        public event HiliteColourChangedEventHandler HiliteColourChanged;
        public delegate void HiliteColourChangedEventHandler(Color colour);
        public void OnHiliteColourChanged(Color colour)
        {
            if (HiliteColourChanged != null)
            {
                HiliteColourChanged(colour);
            }
        }

        public event ThicknessChangedEventHandler ThicknessChanged;
        public delegate void ThicknessChangedEventHandler(double thickness);
        public void OnThicknessChanged(double thickness)
        {
            if (ThicknessChanged != null)
            {
                ThicknessChanged(thickness);
            }
        }

        public event TipChangedEventHandler TipChanged;
        public delegate void TipChangedEventHandler(StylusTip tip);
        public void OnTipChanged(StylusTip tip)
        {
            if (TipChanged != null)
            {
                TipChanged(tip);
            }
        }

        public event EraseModeChangedEventHandler EraseModeChanged;
        public delegate void EraseModeChangedEventHandler(InkCanvasEditingMode mode);
        public void OnEraseModeChanged(InkCanvasEditingMode mode)
        {
            if (EraseModeChanged != null)
            {
                EraseModeChanged(mode);
            }
        }
        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

        #region Show current settings
        internal void SetPenColour(Color colour)
        {
            BlackColour.IsChecked = false;
            RedColour.IsChecked = false;
            BlueColour.IsChecked = false;

            if (colour == Colors.Black)
            {
                BlackColour.IsChecked = true;
            }
            if (colour == Colors.Red)
            {
                RedColour.IsChecked = true;
            }
            if (colour == Colors.Blue)
            {
                BlueColour.IsChecked = true;
            }
        }

        internal void SetHiliteColour(Color colour)
        {
            YellowColour.IsChecked = false;
            FuchsiaColour.IsChecked = false;
            LimeColour.IsChecked = false;

            if (colour == Colors.Yellow)
            {
                YellowColour.IsChecked = true;
            }
            if (colour == Colors.Fuchsia)
            {
                FuchsiaColour.IsChecked = true;
            }
            if (colour == Colors.Lime)
            {
                LimeColour.IsChecked = true;
            }
        }

        internal void SetThickness(double thickness)
        {
            ThinWidth.IsChecked = false;
            MediumWidth.IsChecked = false;
            ThickWidth.IsChecked = false;

            thickness = Math.Floor(thickness);
            if (thickness == 2.0)
            {
                ThinWidth.IsChecked = true;
            }
            if (thickness == 3.0)
            {
                MediumWidth.IsChecked = true;
            }
            if (thickness == 4.0)
            {
                ThickWidth.IsChecked = true;
            }
        }

        internal void SetTip(StylusTip stylusTip)
        {
            SquareTip.IsChecked = false;
            RoundTip.IsChecked = false;

            if (stylusTip == StylusTip.Rectangle)
            {
                SquareTip.IsChecked = true;
            }
            if (stylusTip == StylusTip.Ellipse)
            {
                RoundTip.IsChecked = true;
            }
        }
        #endregion

        #region CustomColour event handler
        private void CustomColour_ColourSelected(object sender, RoutedEventArgs e)
        {
            OnPenColourChanged(CustomColourSelected);
            NotifyPropertyChanged("SelectedPenBrush");
            CustomColour.IsChecked = true;
        }

        private void CustomColour_Checked(object sender, RoutedEventArgs e)
        {
            OnPenColourChanged(CustomColourSelected);
            NotifyPropertyChanged("SelectedPenBrush");
        }
        #endregion
    }
}
